package com.hm.shopping.web.buyer;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hm.common.su.bean.PageInfo;
import com.hm.common.util.CommonUtil;
import com.hm.shopping.auto.SystemEnvConfigProperties;
import com.hm.shopping.auto.su.BaseController;
import com.hm.shopping.auto.su.R.Uri.Shopping;
import com.hm.shopping.auto.su.R.Uri.Shopping.BuyerPersonalCenter;
import com.hm.shopping.auto.su.helper.HmSessionFactory;
import com.hm.shopping.def.OrderStateEnum;
import com.hm.shopping.service.OrderService;
import com.hm.shopping.service.ShopcartService;
import com.hm.shopping.service.UserService;
import com.hm.shopping.vo.ShopcartVo;
import com.hm.shopping.vo.su.SubmitOrderInfoVo;

/**
 * @author shishun.wang
 * @date 2018年1月23日 下午5:10:45
 * @version 1.0
 * @describe 买家个人中心
 */
@Controller
@RequestMapping(Shopping.BUYER_PERSONAL_CENTER)
public class PersonalCenterController extends BaseController {

	@Autowired
	private UserService userService;

	@Autowired
	private OrderService orderService;

	@Autowired
	private ShopcartService shopcartService;

	@Autowired
	private SystemEnvConfigProperties systemEnvConfigProperties;

	/**
	 * 个人中心
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = BuyerPersonalCenter.PERSONAL_CENTER, method = RequestMethod.GET)
	public String personalCenter(Model model) {
		super.commonAttribute(model, systemEnvConfigProperties);

		model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(HmSessionFactory.currentUserIdString()));
		model.addAttribute("stayPayment", orderService.getBuyerGoodsSnapshots(HmSessionFactory.currentUserIdString(), OrderStateEnum.STAY_PAYMENT).size());// 待付款
		model.addAttribute("finishPayment", orderService.getBuyerGoodsSnapshots(HmSessionFactory.currentUserIdString(), OrderStateEnum.FINISH_PAYMENT).size());// 待发货
		model.addAttribute("finishCourier", orderService.getBuyerGoodsSnapshots(HmSessionFactory.currentUserIdString(), OrderStateEnum.FINISH_COURIER).size());// 待收货
		model.addAttribute("refund", orderService.getBuyerGoodsSnapshots(HmSessionFactory.currentUserIdString(), OrderStateEnum.REFUND).size());// 退换货

		return "buyer/personal-center";
	}

	/**
	 * 个人信息
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = BuyerPersonalCenter.PERSONAL_INFO, method = RequestMethod.GET)
	public String personalInfo(Model model) {

		super.commonAttribute(model, systemEnvConfigProperties);
		model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(HmSessionFactory.currentUserIdString()));
		return "buyer/personal-info";
	}

	/**
	 * 个人收货地址
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = BuyerPersonalCenter.PERSONAL_ADDRESS, method = RequestMethod.GET)
	public String personalAddress(Model model) {

		super.commonAttribute(model, systemEnvConfigProperties);
		model.addAttribute("userAddress", userService.getUserAddressList(HmSessionFactory.currentUserId()));
		model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(HmSessionFactory.currentUserIdString()));
		return "buyer/personal-address";
	}

	/**
	 * 安全设置
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = BuyerPersonalCenter.PERSONAL_SAFETY, method = RequestMethod.GET)
	public String personalSafety(Model model) {

		super.commonAttribute(model, systemEnvConfigProperties);
		model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(HmSessionFactory.currentUserIdString()));
		return "buyer/personal-safety";
	}

	/**
	 * 订单管理
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = BuyerPersonalCenter.PERSONAL_ORDER, method = RequestMethod.GET)
	public String personalOrder(Model model) {

		super.commonAttribute(model, systemEnvConfigProperties);
		model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(HmSessionFactory.currentUserIdString()));
		return "buyer/personal-order";
	}

	/**
	 * 退款管理
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = BuyerPersonalCenter.PERSONAL_REFUND, method = RequestMethod.GET)
	public String personalRefund(Model model) {

		super.commonAttribute(model, systemEnvConfigProperties);
		model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(HmSessionFactory.currentUserIdString()));
		return "buyer/personal-refund";
	}

	/**
	 * 优惠券
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = BuyerPersonalCenter.PERSONAL_COUPONS, method = RequestMethod.GET)
	public String personalCoupons(Model model) {

		super.commonAttribute(model, systemEnvConfigProperties);
		model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(HmSessionFactory.currentUserIdString()));
		return "buyer/personal-coupons";
	}

	/**
	 * 红包
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = BuyerPersonalCenter.PERSONAL_BONUS, method = RequestMethod.GET)
	public String personalBonus(Model model) {

		super.commonAttribute(model, systemEnvConfigProperties);
		model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(HmSessionFactory.currentUserIdString()));
		return "buyer/personal-bonus";
	}

	/**
	 * 账单明细
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = BuyerPersonalCenter.PERSONAL_BILL, method = RequestMethod.GET)
	public String personalBill(Model model) {

		super.commonAttribute(model, systemEnvConfigProperties);
		model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(HmSessionFactory.currentUserIdString()));
		return "buyer/personal-bill";
	}

	/**
	 * 收藏
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = BuyerPersonalCenter.PERSONAL_COLLECTION, method = RequestMethod.GET)
	public String personalCollection(Model model) {

		super.commonAttribute(model, systemEnvConfigProperties);
		model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(HmSessionFactory.currentUserIdString()));
		return "buyer/personal-collection";
	}

	/**
	 * 评价
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = BuyerPersonalCenter.PERSONAL_COMMENT, method = RequestMethod.GET)
	public String personalComment(Model model) {

		super.commonAttribute(model, systemEnvConfigProperties);
		model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(HmSessionFactory.currentUserIdString()));
		return "buyer/personal-comment";
	}

	/**
	 * 消息
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = BuyerPersonalCenter.PERSONAL_NEWS, method = RequestMethod.GET)
	public String personalNews(Model model) {

		super.commonAttribute(model, systemEnvConfigProperties);
		model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(HmSessionFactory.currentUserIdString()));
		return "buyer/personal-news";
	}

	/**
	 * 购物车
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = BuyerPersonalCenter.PERSONAL_SHOPCART, method = RequestMethod.GET)
	public String personalShopcart(Model model, @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum) {
		super.commonAttribute(model, systemEnvConfigProperties);
		List<ShopcartVo> shopcartList = shopcartService.getShopcartList(new PageInfo.PageInfoParam(pageNum, 490), HmSessionFactory.currentUserIdString()).getContent();
		model.addAttribute("shopcartList", shopcartList);
		// 勾选中商品数量
		AtomicInteger selectedCount = new AtomicInteger(0);
		// 勾选中商品总价
		AtomicLong selectedGoodsPrice = new AtomicLong(0);
		for (ShopcartVo item : shopcartList) {
			if (item.isSelected()) {
				selectedCount.addAndGet(item.getQuantity());
				selectedGoodsPrice.addAndGet(item.getGoodsIntroduction().getPromotionUnitPrice() * item.getQuantity());
			}
		}
		model.addAttribute("selectedCount", selectedCount.get());
		model.addAttribute("selectedGoodsPrice", selectedGoodsPrice.get());
		model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(HmSessionFactory.currentUserIdString()));

		return "buyer/personal-shopcart";
	}

	/**
	 * 购物车支付
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = BuyerPersonalCenter.PERSONAL_PAY, method = RequestMethod.GET)
	public String personalPay(Model model) {
		super.commonAttribute(model, systemEnvConfigProperties);

		List<ShopcartVo> content = shopcartService.getShopcartList(new PageInfo.PageInfoParam(1, 490), HmSessionFactory.currentUserIdString()).getContent();
		List<ShopcartVo> shopcartList = content.parallelStream().filter(item -> item.isSelected()).collect(Collectors.toList());

		model.addAttribute("shopcartList", shopcartList);
		// 勾选中商品数量
		AtomicInteger selectedCount = new AtomicInteger(0);
		// 勾选中商品总价
		AtomicLong selectedGoodsPrice = new AtomicLong(0);
		for (ShopcartVo item : shopcartList) {
			if (item.isSelected()) {
				selectedCount.addAndGet(item.getQuantity());
				selectedGoodsPrice.addAndGet(item.getGoodsIntroduction().getPromotionUnitPrice() * item.getQuantity());
			}
		}
		model.addAttribute("selectedCount", selectedCount.get());
		model.addAttribute("selectedGoodsPrice", selectedGoodsPrice.get());
		model.addAttribute("userAddress", userService.getUserAddressList(HmSessionFactory.currentUserId()));
		model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(HmSessionFactory.currentUserIdString()));

		if (CommonUtil.isEmpty(shopcartList)) {
			model.addAttribute("orderId", null);
			return "buyer/personal-pay";
		}
		// 生成订单
		String snapshotId = orderService.submitOrder(HmSessionFactory.currentUserIdString(), shopcartList.parallelStream().map(item -> {
			SubmitOrderInfoVo orderInfoVo = new SubmitOrderInfoVo();
			orderInfoVo.setShopcartId(item.getId());
			return orderInfoVo;
		}).collect(Collectors.toList()));
		model.addAttribute("snapshotId", snapshotId);

		return "buyer/personal-pay";
	}

	/**
	 * 用户订单支付
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = BuyerPersonalCenter.PERSONAL_ORDER_PAY, method = RequestMethod.GET)
	public String personalOrderPay(Model model, String snapshotId) {
		super.commonAttribute(model, systemEnvConfigProperties);

		model.addAttribute("shopcartGoodsCount", 0);
		model.addAttribute("payAmount", orderService.personalOrderPay(snapshotId));
		model.addAttribute("userAddress", userService.getUserAddressList(HmSessionFactory.currentUserId()));
		return "buyer/personal-success";
	}

	/**
	 * 收藏列表数据展示
	 * @param model
	 * @return
	 */
	@RequestMapping(value = BuyerPersonalCenter.PERSONAL_FAVORITES, method = RequestMethod.GET)
	public String personalFavorites(Model model) {
		super.commonAttribute(model, systemEnvConfigProperties);

		return "buyer/personal-success";
	}
}
